package com.fish.common.constant;

import java.io.Serializable;

/**
 * 查询分页信息
 */
public class QueryPage implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -804384863178315819L;

	public QueryPage() {
		
	}
	public QueryPage(long pageSize) {
		this.pageSize = pageSize;
	}

	public QueryPage(long totalItem, long pageSize) {
		this.init(totalItem, pageSize);
	}
	public QueryPage(long totalItem, long pageSize, long curPage) {
		this.init(totalItem, pageSize, curPage);
	}
	
	public void init(long totalItem, long pageSize) {
		this.totalItem = totalItem;
		this.itemCount = this.pageSize = pageSize;
		
		this.goPage(1);
	}
	
	public void init(long totalItem, long pageSize, long curPage) {
		this.totalItem = totalItem;
		this.itemCount = this.pageSize = pageSize;
		this.beginItem = this.pageSize*(curPage-1);	
		this.curPage = curPage;
		this.goPage(curPage);		
	}

	/**
	 * 到第curPage页
	 * 
	 * @method goPage
	 * @param curPage
	 */
	public long goPage(long curPage) {
		this.pageCount = this.calPageCount(this.totalItem, this.pageSize);
		if(this.pageCount == 0) return 1;
		long pageNo = (Math.abs(curPage)-1) % this.pageCount + 1;
		if(curPage <= 0) {
			pageNo = this.pageCount - pageNo;
		}
		//this.curPage = (pageNo > this.pageCount) ? this.pageCount : pageNo;
		
		this.itemCount = this.calPageSize(this.totalItem, this.pageSize, this.curPage);
		this.beginItem = this.pageSize*(this.curPage-1);		
		this.endItem = this.beginItem + this.itemCount - 1;
		
		this.refreshGroup();
		
		return this.curPage;
	}
	
	/**
	 * 到首页
	 * @method firstPage
	 * @return
	 */
	public long firstPage() {
		return this.goPage(1);
	}

	/**
	 * 到前一页
	 * @method prePage
	 * @return
	 */
	public long prePage() {
		return this.goPage(this.curPage-1);
	}

	/**
	 * 到下一页
	 * @method nextPage
	 * @return
	 */
	public long nextPage() {
		return this.goPage(this.curPage+1);
	}

	/**
	 * 到最后一页
	 * @method lastPage
	 * @return
	 */
	public long lastPage() {
		return this.goPage(this.pageCount);
	}

	/**
	 * 查询条件对应的总条目数
	 */
	private long totalItem = 0;

	/**
	 * 总页数
	 */
	private long pageCount = 0;

	/**
	 * 分页尺寸
	 */
	private long pageSize = 20;

	/**
	 * 当前页序号
	 */
	private long curPage = 1;

	/**
	 * 开始条目
	 */
	private long beginItem = 0;

	/**
	 * 结束条目
	 */
	private long endItem = 0;

	/**
	 * 本页条目数
	 */
	private long itemCount = 0;

//	/**
//	 * 计算本页条目数
//	 * 
//	 * @method calItemCount
//	 * @return
//	 */
//	private long calItemPageSize(long totalItem, long pageSize, long beginItem) {
//		long itemCount = 0;
//		long pageCount = this.calPageCount(totalItem, pageSize);
//		long curPage = this.calItemPage(pageSize, beginItem);
//		if (curPage < pageCount) {
//			itemCount = pageSize;
//		} else {
//			itemCount = totalItem - (pageSize * (curPage - 1));
//		}
//		return itemCount;
//	}

	/**
	 * 计算本页条目数
	 * 
	 * @method calPageSize
	 * @param totalItem
	 * @param pageSize
	 * @param curPage
	 * @return
	 */
	private long calPageSize(long totalItem, long pageSize, long curPage) {
		long itemCount = 0;
		long pageCount = this.calPageCount(totalItem, pageSize);
		if (curPage < pageCount) {
			itemCount = pageSize;
		} else {
			itemCount = totalItem - (pageSize * (curPage - 1));
		}
		return itemCount;
	}

	/**
	 * 计算当前页
	 * 
	 * @method calCurPage
	 * @return
	 */
	public static long calItemPage(long pageSize, long beginItem) {
		if (pageSize == 0)
			return 1;
		return (beginItem / pageSize) + 1;
	}

	/**
	 * 计算总页数
	 * 
	 * @method calPageCount
	 * @return
	 */
	private long calPageCount(long totalItem, long pageSize) {
		long pageCount = 0;
		if (pageSize == 0) {
			// 此时不分页
			pageCount = 1;
		} else {
			pageCount = (long) Math.ceil(((double) totalItem)
					/ ((double) pageSize));
		}
		return pageCount;
	}

	public long getBeginItem() {
		return beginItem;
	}

	public void setBeginItem(long beginItem) {
		this.beginItem = beginItem;
	}

	public long getCurPage() {
		return curPage;
	}

	public void setCurPage(long curPage) {
		if(curPage<1) {
			this.curPage = 1;
		}
		this.curPage = curPage;
	}
	
	public long getEndItem() {
		return endItem;
	}

	public void setEndItem(long endItem) {
		this.endItem = endItem;
	}

	public long getPageSize() {
		return pageSize;
	}

	public void setPageSize(long pageSize) {
		this.pageSize = pageSize;
	}

	public long getTotalItem() {
		return totalItem;
	}

	public void setTotalItem(long totalItem) {
		this.totalItem = totalItem;
	}

	public long getPageCount() {
		return pageCount;
	}

	public void setPageCount(long pageCount) {
		this.pageCount = pageCount;
	}

	public long getItemCount() {
		return itemCount;
	}

	public void setItemCount(long itemCount) {
		this.itemCount = itemCount;
	}
	
	public String toText() {
		StringBuffer bufText = new StringBuffer();
		bufText.append("totalItem=").append(this.totalItem).append(";")
			.append("pageSize=").append(this.pageSize).append(";")
			.append("pageCount=").append(this.pageCount).append(";")
			.append("curPage=").append(this.curPage).append(";")
			.append("itemCount=").append(this.itemCount).append(";")
			.append("endItem=").append(this.endItem).append(";");
		
		return bufText.toString();
	}
	
	/**
	 * 页面分组相关
	 */
	private long groupSize = 10; //显示分组大小
	
	private long startPage; //当前开始页码
	
	private long endPage; //当前结束页码
	
	public long getGroupSize() {
		return groupSize;
	}

	public void setGroupSize(long groupSize) {
		this.groupSize = groupSize;
	}
	
	/**
	 * @return
	 */
	public long getEndPage() {
		return endPage;
	}

	/**
	 * @return
	 */
	public long getStartPage() {
		return startPage;
	}

	/**
	 * @param i
	 */
	public void setEndPage(long i) {
		endPage = i;
	}

	/**
	 * @param i
	 */
	public void setStartPage(long i) {
		startPage = i;
	}
	
	public void refreshGroup() {
		long pageGroupNumber = 0;
		long pagesGroups = 0;
		
		// 计算总组数
		if (this.pageCount / this.groupSize == 0) {
			pagesGroups = 1;
		} else if (this.pageCount / this.groupSize >= 1 && this.pageCount % this.groupSize == 0) {
			pagesGroups = this.pageCount / this.groupSize;
		} else if (this.pageCount / this.groupSize >= 1 && this.pageCount % this.groupSize != 0) {
			pagesGroups = this.pageCount / groupSize + 1;
		}
		
		// 计算当前的组号
		if (this.curPage / this.groupSize == 0) {
			pageGroupNumber = 1;
		} else if (this.curPage / this.groupSize >= 1 && this.curPage % this.groupSize == 0) {
			pageGroupNumber = this.curPage / this.groupSize;
		} else if (this.curPage / this.groupSize >= 1 && this.curPage % this.groupSize != 0) {
			pageGroupNumber = this.curPage / this.groupSize + 1;
		}
		
		//首组页且小于一页
		if (pageGroupNumber == 1 && this.pageCount < this.groupSize) {
			this.setStartPage(1);
			this.setEndPage(this.getPageCount());
		}
		// 其它组页
		else if (pageGroupNumber >= 1 && pageGroupNumber < pagesGroups) {
			this.setStartPage((pageGroupNumber - 1) * this.groupSize + 1);
			this.setEndPage(this.getStartPage() + this.groupSize -1);
		}
		//末组页
		else if (pageGroupNumber == pagesGroups) {
			// 最后一组页为满页groupSize个
			this.setStartPage((pageGroupNumber - 1) * this.groupSize + 1);
			this.setEndPage(this.getPageCount());
		}
	}
	
	public static void main(String[] args) {
		QueryPage page = new QueryPage(43, 20, 1);
		System.out.println("总数:" + page.getTotalItem() + ",页大小:"
				+ page.getPageSize() + ",当前页:" + page.getCurPage());
		System.out.println("总页数=" + page.getPageCount());
		System.out.println("当前页=" + page.getCurPage());
		System.out.println("当前页条目数=" + page.getItemCount());
		System.out.println("当前页开始条目=" + page.getBeginItem());
		System.out.println("当前页结束条目=" + page.getEndItem());
System.out.println("-----------------------------------------------");
		page.nextPage();
		System.out.println("总数:" + page.getTotalItem() + ",页大小:"
				+ page.getPageSize() + ",当前页:" + page.getCurPage());
		System.out.println("总页数=" + page.getPageCount());
		System.out.println("当前页=" + page.getCurPage());
		System.out.println("当前页条目数=" + page.getItemCount());
		System.out.println("当前页开始条目=" + page.getBeginItem());
		System.out.println("当前页结束条目=" + page.getEndItem());
		
System.out.println("-----------------------------------------------");
		page.prePage();
		page.prePage();
		System.out.println("总数:" + page.getTotalItem() + ",页大小:"
				+ page.getPageSize() + ",当前页:" + page.getCurPage());
		System.out.println("总页数=" + page.getPageCount());
		System.out.println("当前页=" + page.getCurPage());
		System.out.println("当前页条目数=" + page.getItemCount());
		System.out.println("当前页开始条目=" + page.getBeginItem());
		System.out.println("当前页结束条目=" + page.getEndItem());
		
System.out.println("-----------------------------------------------");
		page.firstPage();
		System.out.println("总数:" + page.getTotalItem() + ",页大小:"
				+ page.getPageSize() + ",当前页:" + page.getCurPage());
		System.out.println("总页数=" + page.getPageCount());
		System.out.println("当前页=" + page.getCurPage());
		System.out.println("当前页条目数=" + page.getItemCount());
		System.out.println("当前页开始条目=" + page.getBeginItem());
		System.out.println("当前页结束条目=" + page.getEndItem());
		
System.out.println("-----------------------------------------------");
		page.lastPage();
		System.out.println("总数:" + page.getTotalItem() + ",页大小:"
				+ page.getPageSize() + ",当前页:" + page.getCurPage());
		System.out.println("总页数=" + page.getPageCount());
		System.out.println("当前页=" + page.getCurPage());
		System.out.println("当前页条目数=" + page.getItemCount());
		System.out.println("当前页开始条目=" + page.getBeginItem());
		System.out.println("当前页结束条目=" + page.getEndItem());
		
		System.out.println(page.toText());
	}
}
